<%
layout("tabbar.html",{index: 1}){
%>
<div id="rechargesList">

</div>
<a id="loadMoreButton" style="margin-top: 50px;" href="javascript:" class="weui-btn weui-btn_default">加载更多</a>
<script src="${ctxPath}/js/net.js"></script>
<script>
    var page = 1;
    var days = [];

    function fetchData(page) {
        net().url('${ctxPath}/api/user/oilRecharges?page=' + page)
            .success(function (response) {
                if (response.code == 0) {
                    if (response.data.length == 0) {
                        alert('没有更多数据');
                    } else {
                        appendToList(response.data);
                    }
                } else {
                    alert(response.message);
                }
            })
            .error(function (error) {
                alert('网络错误');
            })
            .get();
    }

    function appendToList(list) {
        list.forEach(function (t) {
            console.log(t.createTime);
            var date = new Date(t.createTime);
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var day = date.getDate();
            var fullDayStr = year + '-' + month+ '-' + day;
            var dayStr = month + '-' + day;
            var status = null;
            switch (t.status) {
                case 'Submit':
                    status = '已提交';
                    break;
                case 'Success':
                    status = '成功';
                    break;
                case 'Fail':
                    status = '失败';
                    break;
            }
            if (days.indexOf(fullDayStr) >= 0) {
                //已经存在的日期，直接加入
                appendToView(fullDayStr, t.cardNo, t.money / 100, status);
            } else {
                //加入存在日期
                days.push(fullDayStr);
                $('#rechargesList').append('<div class="weui-cells__title">' + dayStr +'</div>');
                $('#rechargesList').append('<div class="weui-cells" id="' + fullDayStr + '"></div>');
                appendToView(fullDayStr, t.cardNo, t.money / 100, status);
            }
        })
    }

    function appendToView(fullDayStr, cardNo, money, status) {
    	if(status=='失败'){
        	$('#' + fullDayStr).append('<div class="weui-cell"><div class="weui-cell__bd"><p>' + cardNo + '<span style="font-size: 10px;margin-left: 10px; color:red">' + status + '</span>' +'</p></div><div class="weui-cell__ft">' + money + '元' +'</div></div>');
    	}else{
	        $('#' + fullDayStr).append('<div class="weui-cell"><div class="weui-cell__bd"><p>' + cardNo + '<span style="font-size: 10px;margin-left: 10px;">' + status + '</span>' +'</p></div><div class="weui-cell__ft">' + money + '元' +'</div></div>');
    	}
    }

    fetchData(page);
    
    $('#loadMoreButton').click(function () {
        page ++;
        fetchData(page);
    });

</script>
<% } %>